package com.sinopec.resmodule.widget;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.util.SmartUtil;

public class GridSpaceItemDecoration extends RecyclerView.ItemDecoration {

    private int horizontal, vertical, size;

    public GridSpaceItemDecoration(int horizontal, int vertical, int size) {
        this.horizontal = SmartUtil.dp2px(horizontal) / 2;
        this.vertical = SmartUtil.dp2px(vertical);
        this.size = size;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildLayoutPosition(view);
        if (position >= size) {
            outRect.top = vertical;
        }
        if (position % size == 0) {
            outRect.right = horizontal;
        } else if (position % size == size - 1) {
            outRect.left = horizontal;
        } else {
            outRect.left = horizontal / 2;
            outRect.right = horizontal / 2;
        }
    }
}